//
//  UIButton+ZZUtil.m
//  OCFrame
//
//  Created by hfzhangzhang on 2016/12/19.
//  Copyright © 2016年 hfzhangzhang. All rights reserved.
//

#import "UIButton+ZZUtil.h"
#import "UIColor+ZZUtil.h"

UIImage *imageWithColor(UIColor *color) {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


@implementation UIButton (ZZUtil)

- (void)zz_setBackgroundColor:(UIColor *)color forState:(UIControlState)state
{
   [self setBackgroundImage:imageWithColor(color) forState:state];
}

- (void)setTitleColor:(UIColor *)titleColor {
    [self setTitleColor:titleColor forState:UIControlStateNormal];
    [self setTitleColor:[titleColor zz_highlightedColor] forState:UIControlStateHighlighted];
    [self setTitleColor:[titleColor zz_disabledColor] forState:UIControlStateDisabled];
}

- (UIColor *)titleColor {
    return [self titleColorForState:UIControlStateNormal];
}

- (void)setBackgroundImageColor:(UIColor *)backgroundImageColor {
    [self setBackgroundImage:imageWithColor(backgroundImageColor) forState:UIControlStateNormal];
    [self setBackgroundImage:imageWithColor([backgroundImageColor zz_highlightedColor]) forState:UIControlStateHighlighted];
    [self setBackgroundImage:imageWithColor([backgroundImageColor zz_disabledColor]) forState:UIControlStateDisabled];
}

- (UIColor *)backgroundImageColor {
    return nil;
}


@end



